package com.aabte.design.pattern.creational.singleton;

import java.io.Serializable;

public class HungrySingleton implements Serializable, Cloneable {

    private final static HungrySingleton INSTANCE;

    static {
        INSTANCE = new HungrySingleton();
    }

    private HungrySingleton(){
        if (INSTANCE != null) {
            throw new RuntimeException("单例构造器禁止反射调用！");
        }
    }

    public static HungrySingleton getInstance(){
        return INSTANCE;
    }

    /**
     * 解决序列化破坏单例
     */
    public Object readResolve(){
        return INSTANCE;
    }

    /**
     * 解决克隆破坏单例
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return getInstance();
    }
}
